package com.cll.learn.control.demo05;

import java.util.Random;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Phaser;

/**
 * 描述信息:
 * 学生线程类
 * 等待考试
 * 笔试
 * 面试
 * @author CLL
 * @version 1.0
 * @date 2021/5/15 17:36
 */
public class StudentThread implements Runnable {

    private final String name;

    private final Phaser phaser;

    public StudentThread(String name, Phaser phaser) {
        this.name = name;
        this.phaser = phaser;
    }

    @Override
    public void run() {
        System.out.println("===>>> " + name + "到达，等待开始考试");
        // 到达了，并开启了下一轮
        phaser.arriveAndAwaitAdvance();
        doBiShi();
        phaser.arriveAndAwaitAdvance();
        doMianShi();
//        phaser.arriveAndAwaitAdvance();
    }

    private void doBiShi(){
        System.out.println("===>>> " + name + "开始笔试");
        try {
            Thread.sleep(Math.abs(new Random().nextInt(1000)));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("===>>> " + name + "笔试完成");
    }

    private void doMianShi(){
        System.out.println("===>>> " + name + "开始面试");
        try {
            Thread.sleep(Math.abs(new Random().nextInt(1000)));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("===>>> " + name + "面试完成");
    }
}
